package com.yitao.cms.utils;

import com.skipper.base.utils.DateUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.skipper.base.utils.DateUtil.DATE_TIME_PATTERN;

/**
 * @Auther: wuwenlong
 * @Date: 2018/6/2 0002 20:03
 * @Description:
 */
public class DateUtils {
    private static final Log logger = LogFactory.getLog(DateUtils.class);

    private static SimpleDateFormat sdf = new SimpleDateFormat();

    /**
     * 获取当前时间
     */
    public static String nowDate(String layout) {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(layout);
        return sdf.format(d);
    }
    /* public static String timeStamp2Date(String )*/

    /**
     * 将格式为yyyy-MM-dd HH:mm:ss的时间字符串转化为Date类型
     *
     * @param strDate 格式为yyyy-MM-dd HH:mm:ss的时间字符串
     * @return java.util.Date
     */
    public static Date parseDateMilliFormat(String strDate) {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        return parseDateFormat(strDate, pattern);
    }

    /**
     * 描述: 比较两个日期大小，如果前者较晚，返回正数，否则返回负数
     */
    public static int compareDate(String time1, String time2) {
        Date date1 = parseDateFormat(time1, "yyyy-MM-dd HH:mm:ss");
        Date date2 = parseDateFormat(time2, "yyyy-MM-dd HH:mm:ss");
        return compareDate(date1, date2);
    }

    /**
     * 描述: 比较两个日期大小，如果前者较晚，返回正数，否则返回负数
     */
    public static int compareDate(Date date1, Date date2) {
        if (date1.getTime() > date2.getTime()) {
            return 1;
        } else {
            return -1;
        }
    }

    /**
     * 输出时间格式为yyyy-MM-dd HH:mm:ss的字符串
     *
     * @return String 输出时间格式为yyyy-MM-dd HH:mm:ss的字符串
     */
    public static String getDateSecondFormat() {
        Date date = new Date();
        String pattern = "yyyy-MM-dd HH:mm:ss SSS";
        return getDateFormat(date, pattern);
    }

    /**
     * 输出时间格式为yyyy-MM-dd HH:mm:ss的字符串
     *
     * @param date Date
     * @return String 输出时间格式为yyyy-MM-dd HH:mm:ss的字符串
     */
    public static String getDateSecondFormat(Date date) {
        if (date != null) {
            String pattern = "yyyy-MM-dd HH:mm:ss";
            return getDateFormat(date, pattern);
        } else {
            return null;
        }

    }
    /**
     * 输出时间格式为yyyy-MM-dd HH:mm:ss的字符串
     *
     * @param date Date
     * @return String 输出时间格式为yyyy-MM-dd HH:mm:ss的字符串
     */
    public static String getDateSecondFormatCashCoupons(Date date) {
        if (date != null) {
            String pattern = "yyyy.MM.dd HH:mm";
            return getDateFormat(date, pattern);
        } else {
            return null;
        }

    }

    /**
     * 根据时间字符串和对应的格式输出时间(Date)类型,如果时间字符串和格式不匹配则出现异常
     *
     * @param strDate 时间字符串
     * @param pattern 时间字符串对应的格式pattern
     * @return java.util.Date
     */
    public static Date parseDateFormat(String strDate, String pattern) {
        if (strDate == null || strDate.trim().length() == 0) {
            return null;
        }
        synchronized (sdf) {
            StringBuffer datebuf = new StringBuffer();
            datebuf.append(strDate);
            if (strDate.length() == 10) {
                datebuf.deleteCharAt(7);
                datebuf.deleteCharAt(4);
            }
            Date date = null;
            sdf.applyPattern(pattern);
            try {
                date = sdf.parse(datebuf.toString());
            } catch (Exception e) {
                logger.error("DataUntil", e);
            }
            return date;
        }
    }

    /**
     * 将时间戳转换为时间
     */
    public static String stampToDate(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return (simpleDateFormat.format(date));
    }

    /**
     * 根据日期(Date)和日期格式返回对应的字符串
     *
     * @param date    Date类型日期
     * @param pattern 传入的日期格式(例如：yyyy-MM-dd HH:mm:ss)
     * @return String 日期字符串
     */
    public static String getDateFormat(Date date, String pattern) {
        if (date == null)
            return null;
        synchronized (sdf) {
            String str = null;
            sdf.applyPattern(pattern);
            str = sdf.format(date);
            return str;
        }
    }

    /**
     * 获取当前日期前一天日期
     *
     * @return
     */
    public static String getYesterdayDate() {
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date());
        ca.add(Calendar.DATE, -1);//获取前一天日期
        Date lastDay = ca.getTime();//结果
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(lastDay);
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static Date getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        return today;
    }

    //获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    //获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    public static void main(String[] args) {

        String index = "2019-08-08 23:40:01";
        Date date1 = DateUtils.parseDateMilliFormat(index);

        Date pastDate = DateUtils.getPastDate(-1);
        Date date = new Date();
        String dateFormat = getDateFormat(date, DatetimeFormat.STANDARED_DATE_FORMAT.getFormat());
        Date startDate = DateUtil.parseDate(dateFormat + " 23:40:00", DATE_TIME_PATTERN);
        System.out.print(startDate);
        String dateFormat1 = getDateFormat(pastDate, DatetimeFormat.STANDARED_DATE_FORMAT.getFormat());
        Date endDate = DateUtil.parseDate(dateFormat1 + " 00:00:30", DATE_TIME_PATTERN);
        System.out.println(endDate);

        if (date1.getTime() < endDate.getTime() && date1.getTime() > startDate.getTime()) {
            System.out.println("联动支付  23.45 - 00.25 不支持退款 ");
        }


    }
}
